<!--
 * @Author: kenan
 * @Date: 2022-09-19 20:38:50
 * @LastEditors: kenan
 * @LastEditTime: 2022-09-22 11:29:57
 * @FilePath: /smartide-server/web/index.html
 * @Description: 
 * 
 * Copyright (c) 2022 by kenanlu@leansoftx.com, All Rights Reserved. 
-->
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width,initial-scale=1.0">
    <meta
        content="Gin,Vue,Admin.Gin-Vue-Admin,GVA,gin-vue-admin,后台管理框架,vue后台管理框架,gin-vue-admin文档,gin-vue-admin首页,gin-vue-admin"
        name="keywords" />
    <link rel="icon" href="favicon.ico">
    <title></title>
    <script>
        function success (text) {
             var conf= JSON.parse(text)

            if (conf && conf.code == 0 && conf.data.assetsUrl) {
                var link = document.querySelector("link[rel*='icon']") || document.createElement('link');
                link.type = 'image/x-icon';
                link.rel = 'shortcut icon';
                //https://smartidedl.blob.core.chinacloudapi.cn/oem/favicon.ico text.data.assetsUrl + '/favicon.ico';
                link.href = conf.data.assetsUrl + '/favicon.ico';
                document.getElementsByTagName('head')[0].appendChild(link);
            }
        }

        function fail (code) {
            console.log('getConf fail')
        }

        var request = new XMLHttpRequest(); // 新建XMLHttpRequest对象

        request.onreadystatechange = function () { // 状态发生变化时，函数被回调
            if (request.readyState === 4) { // 成功完成
                // 判断响应结果:
                if (request.status === 200) {
                    // 成功，通过responseText拿到响应的文本:
                    // console.log(request)
                    return success(request.responseText);
                } else {
                    // 失败，根据响应码判断失败原因:
                    return fail(request.status);
                }
            } else {
                // HTTP请求还在继续...
            }
        }

        window.global = window

        function handleOpenInSmartIDE () {
            console.log("access url:", document.location.href);
            if (document.location.hash.startsWith("#/http") == false
                && document.location.hash.startsWith("#http") == false
                && document.location.hash.startsWith("#git@") == false
                && document.location.hash.startsWith("#/git@") == false)
                return
            let url = "";
            try {
                let currentURL = new URL(document.location.hash.replace("#/http", "http").replace("#http", "http"));
                url = currentURL.href;
            }
            catch (ex) {
                console.log("handleOpenInSmartIDE Invited Git url:", url, ex.message);
                if (document.location.hash.startsWith("#git@") || document.location.hash.startsWith("#/git@")) {
                    url = document.location.hash.replace("#/git@", "git@").replace("#git@", "git@");
                }
                else
                    return;
            }
            if (url != "") {
                const localUrl = new URL("?gitRepoUrl=" + url, window.location.origin);
                const workSpaceUrl = new URL("/#/layout/smartide/workspace?gitRepoUrl=" + url, window.location.origin);
                //const queryObj = Object.fromEntries(localUrl.searchParams);  
                //console.log(localUrl,workSpaceUrl,queryObj);
                document.location.href = workSpaceUrl;
            }
        }

        handleOpenInSmartIDE();

        (function () {
            // 发送请求:
            request.open('GET', '/api/smartide/base/getConf');
            request.send();

        })();
    </script>
</head>

<body>
    <div id="app"></div>
    <script type="module" src="./src/main.js"></script>
</body>

</html>